package com.myapp.Activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.gizwits.gizwifisdk.api.GizWifiSDK;
import com.gizwits.gizwifisdk.enumration.GizWifiErrorCode;
import com.gizwits.gizwifisdk.listener.GizWifiSDKListener;
import com.myapp.R;
import com.myapp.View.SimpleToolbar;
import com.myapp.utils.ClickUtils;
import com.myapp.utils.PreferencesUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.myapp.Activity.LoginActivity.LOGIN_NAME;
import static com.myapp.Activity.LoginActivity.TOKEN;

/**
 * author : YuFeng
 * e-mail : 923462992@qq.com
 * date   : 2019/8/1415:06
 * desc   :
 */
public class ChangePasswordActivity extends BaseActivity {
    private static final String TAG = "ChangePasswordActivity";

    private String name;
    @BindView(R.id.toolbar_change_password)
    SimpleToolbar toolbarChangePassword;
    @BindView(R.id.et_user_old_code)
    EditText etUserOldCode;
    @BindView(R.id.et_user_new_code)
    EditText etUserNewCode;
    @BindView(R.id.et_user_new_code_again)
    EditText etUserNewCodeAgain;
    @BindView(R.id.cc_user_confirm)
    ConstraintLayout ccUserConfirm;
    @BindView(R.id.tv_user_tel)
    TextView tvUserTel;
    private String token;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.acitivity_change_password);
        token = PreferencesUtil.getInstance(getApplicationContext()).getString(TOKEN, null);
        ButterKnife.bind(this);
        toolbarChangePassword.setLeftIconClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        name = PreferencesUtil.getInstance(getApplicationContext()).getString(LOGIN_NAME,null);
        if (name!=null){
            tvUserTel.setText(name);
        } else {
            tvUserTel.setText(" ");
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @OnClick(R.id.cc_user_confirm)
    public void onViewClicked() {
        if (ClickUtils.isFastDoubleClick()) {
            return;
        }

        String inputCode = etUserOldCode.getText().toString();
        String inputPassword = etUserNewCode.getText().toString();
        String againPassword = etUserNewCodeAgain.getText().toString();
        if(inputPassword.equals(againPassword)) {
            GizWifiSDK.sharedInstance().setListener(mListener);
            // 调用SDK的邮箱号注册接口
            GizWifiSDK.sharedInstance().changeUserPassword(token, inputCode, inputPassword);
        } else {
            Toast.makeText(getApplicationContext(),"两次密码不同",Toast.LENGTH_SHORT).show();
        }
    }

    GizWifiSDKListener mListener = new GizWifiSDKListener() {
        @Override
        public void didChangeUserPassword(GizWifiErrorCode result) {
            super.didChangeUserPassword(result);
            if (result == GizWifiErrorCode.GIZ_SDK_SUCCESS) {
                Toast.makeText(getApplicationContext(),"更改成功",Toast.LENGTH_SHORT).show();
                // 注册成功
                finish();
            } else {
                Toast.makeText(getApplicationContext(),"更改失败",Toast.LENGTH_SHORT).show();
                // 注册失败
            }
        }
    };
}
